--
module("UI.Windows.ZhuCeWin", package.seeall)

local UIUtility = require("UI.Utility")
local UIWindow = require("UI.UIWindow").UIWindow
local UIWinMgr = require("UI.UIWinMgr").UIWinMgr
local json = require("cjson")
ZhuCeWin = UIWindow:new()

ZhuCeWin.name = "ZhuCeWin"


function ZhuCeWin:Init()
	
	local t = self.Container.transform
	-----注册
	self.ZhuCeButton = UIUtility.GetChildTransform("ZhuCeButton", t, true)
	-----验证
	self.YanZhengButton = UIUtility.GetChildTransform("YanZhengButton", t, true)
	-----hui验证
	self.HuiYanZhengButton = UIUtility.GetChildTransform("HuiYanZhengButton", t, true)
	self.huidaojishi = UIUtility.FindContorl('UILabel', "huidaojishi", t)
	-----关闭注册按钮
	self.ZhuCeBackButton = UIUtility.GetChildTransform("ZhuCeBackButton", t, true)

	self.ZCAccountInput = UIUtility.FindContorl('UIInput', "ZCAccountInput", t)
	self.ZCAccountPassWord = UIUtility.FindContorl('UIInput', "ZCAccountPassWord", t)
	self.ZCAccountYanZheng = UIUtility.FindContorl('UIInput', "ZCAccountYanZheng", t)
	
	self.SurePassWord = UIUtility.FindContorl('UIInput', "SurePassWord", t)

	self.LookMima = UIUtility.GetChildTransform("LookMima", t, true)

	local LoginModule = require("Module.LoginModule").LoginModule
	
	self:BindEvents()
end

function ZhuCeWin:OnShow()
	
end

function ZhuCeWin:OnClose()
	if self.QZCountTimeID ~= nil then
		CountDownMgr.Instance:RemoveCountDown(self.QZCountTimeID, false)
		self.QZCountTimeID = nil
	end
end


function ZhuCeWin:BindEvents()
	-- function OpenPhoneJianPanWin()
	-- 	local JianPanWin = UIWinMgr:OpenWindow("JianPanWin")
	-- 	if self.ZCAccountInput.text ~= "请输入手机号" then
	-- 		JianPanWin:SetShowLabel(self.ZCAccountInput, self.ZCAccountInput.text)
	-- 	else
	-- 		JianPanWin:SetShowLabel(self.ZCAccountInput, "")
	-- 	end
	-- end
	-- UIHelper.BindUIEvnet("Click", OpenPhoneJianPanWin, self.ZCAccountInput.gameObject)

	-- function OpenCodeJianPanWin()
	-- 	local JianPanWin = UIWinMgr:OpenWindow("JianPanWin")
	-- 	if self.ZCAccountYanZheng.text ~= "输入验证码" then
	-- 		JianPanWin:SetShowLabel(self.ZCAccountYanZheng, self.ZCAccountYanZheng.text)
	-- 	else
	-- 		JianPanWin:SetShowLabel(self.ZCAccountYanZheng, "")
	-- 	end
	-- end
	-- UIHelper.BindUIEvnet("Click", OpenCodeJianPanWin, self.ZCAccountYanZheng.gameObject)

	local LoginModule = require("Module.LoginModule").LoginModule
	---------test
	function Zhuce()
		----获取验证码
		-- wwwfrom = UnityEngine.WWWForm()
		
		-- wwwfrom:AddField("u", self.ZCAccountInput.value)

		-- wwwfrom:AddField("t", os.time())
		-- local s = LuaHelper.Instance:MyGetMD5(self.ZCAccountInput.value..os.time().."szFlEKLzYeO36dDn0vW8")
		-- wwwfrom:AddField("k", s)
		
		-- local url = "http://192.168.0.106:8080/qipaiLogin/user!getAuthCode.action"
		
		-- local ConfigModule = require("Module.ConfigModule").ConfigModule
		-- --self.SessionUrl = ConfigModule.SessionUrl
		-- local tempurl = require("NetWork.NetHttp").SendRequest(url, nil, wwwfrom)
		-- Debug.LogErrorEx("jsondata.target~~~~~~~~~~~~~~~~~~1111111111111111111~~~"..stringify(tempurl))

		------注册
		
		

		wwwfrom = UnityEngine.WWWForm()
		wwwfrom:AddField("u", self.ZCAccountInput.value)
		wwwfrom:AddField("ps", self.ZCAccountPassWord.value)
		wwwfrom:AddField("t", os.time())
		local s = LuaHelper.Instance:MyGetMD5(self.ZCAccountInput.value..self.ZCAccountPassWord.value..os.time().."szFlEKLzYeO36dDn0vW8")
		wwwfrom:AddField("k", s)
		wwwfrom:AddField("c", self.ZCAccountYanZheng.value)
		--local url = "http://192.168.0.106:8080/qipaiLogin/user!register.action"
		local ConfigModule = require("Module.ConfigModule").ConfigModule
		--local url = "http://"..ConfigModule.url.."/qipaiLogin/user!register.action"
		local loginurl = ConfigModule.SessionUrl
		local zhuceUrl = string.gsub(loginurl,'user!login','user!register')
		--Debug.LogErrorEx("请求注册地址------------------------"..url)
		--self.SessionUrl = ConfigModule.SessionUrl
		local tempurl = require("NetWork.NetHttp").SendRequest(zhuceUrl, function (isok,strdata)
			if isok then
				local jsondata = json.decode(strdata)
				if jsondata.code == 0 then

					self:Close()
					self.YanZhengButton.gameObject:SetActive(true)
					self.HuiYanZhengButton.gameObject:SetActive(false)
					if self.QZCountTimeID ~= nil then
						CountDownMgr.Instance:RemoveCountDown(self.QZCountTimeID, false)
					end
					local str = UIWinMgr:GetIntTextByCode(72)
					--UIWinMgr:OpenNewGameNotice("注册账号成功")
					UIWinMgr:OpenNewGameNotice(str)

				else
					UIWinMgr:OpenNewGameNotice(jsondata.msg)
				end
				Debug.LogErrorEx("jsondata.target~~~~~~~~~~~~~~~~~~1111111111111111111~~~"..stringify(strdata))
			else
				local str = UIWinMgr:GetIntTextByCode(73)
				UIWinMgr:OpenNewGameNotice(str)
				--UIWinMgr:OpenNewGameNotice("注册账号失败")
			end
		end, wwwfrom)
		Debug.LogErrorEx("jsondata.target~~~~~~~~~~~~~~~~~~1111111111111111111~~~"..stringify(tempurl))
	end
	UIHelper.BindUIEvnet("Click", Zhuce, self.ZhuCeButton.gameObject)

	function Yanzheng()
		
		if self.ZCAccountPassWord.value ~= self.SurePassWord.value then
			local str = UIWinMgr:GetIntTextByCode(66)
			UIWinMgr:OpenNotice(str)
			--UIWinMgr:OpenNotice("两次输入密码不一致！")
			return
		end
		
		wwwfrom = UnityEngine.WWWForm()
		wwwfrom:AddField("u", self.ZCAccountInput.value)
		wwwfrom:AddField("ps", self.ZCAccountPassWord.value)
		local t = os.time()
		wwwfrom:AddField("t", t)
		local s = LuaHelper.Instance:MyGetMD5(self.ZCAccountInput.value..t.."szFlEKLzYeO36dDn0vW8")
		wwwfrom:AddField("k", s)
		local ConfigModule = require("Module.ConfigModule").ConfigModule
		--local url = "http://"..ConfigModule.url.."/qipaiLogin/user!getAuthCode.action"
		local loginurl = ConfigModule.SessionUrl
		local zhuceUrl = string.gsub(loginurl,'user!login','user!getAuthCode')
		--local url = "http://47.106.136.129:8080/qipaiLogin/user!getAuthCode.action"
		
		--self.SessionUrl = ConfigModule.SessionUrl
		local tempurl = require("NetWork.NetHttp").SendRequest(zhuceUrl, function (isok,strdata)
			if isok then
				Debug.LogErrorEx("yyyyyyyyyyyzzzzzzzzzzzz.target~~~~~~~~~~~~~~~~~~~~~"..stringify(strdata))
				local jsondata = json.decode(strdata)

				if jsondata.code ~= 0 then
					UIWinMgr:OpenNewGameNotice(jsondata.msg)
				else
					self.YanZhengButton.gameObject:SetActive(false)
					self.HuiYanZhengButton.gameObject:SetActive(true)
					function QZCountDown(countTime)
						self.huidaojishi.text = countTime
					end
					
					function QZEnd()
						self.QZCountTimeID = nil
						self.YanZhengButton.gameObject:SetActive(true)
						self.HuiYanZhengButton.gameObject:SetActive(false)
					end
					
					self.QZCountTimeID = CountDownMgr.Instance:CreateCountDown(300, 1, QZCountDown, QZEnd)
				end
			else
				
			end
		end, wwwfrom)
	end
	UIHelper.BindUIEvnet("Click", Yanzheng, self.YanZhengButton.gameObject)
	
	
	function CloseZhuce()
		self:Close()
	end
	UIHelper.BindUIEvnet("Click", CloseZhuce, self.ZhuCeBackButton.gameObject)


	function LookMima()
		--Debug.log("---------------look"..self.ZCAccountPassWord.value)
		-- if self.ZCAccountPassWord.inputType == UIInput.InputType.Password then
		-- 	self.ZCAccountPassWord.inputType=UIInput.InputType.Standard
		-- elseif self.ZCAccountPassWord.inputType==UIInput.InputType.Standard then
		-- 	self.ZCAccountPassWord.inputType=UIInput.InputType.Password
		-- end
		
	end
	UIHelper.BindUIEvnet("Click", LookMima, self.LookMima.gameObject)
end

